package string.ReverseStr;

/**
 * @ClassName LeftRotateStr
 * @Description TODO
 * @Author lenovo
 * @Date 2023-02-22 16:52
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class LeftRotateStr {

    /**
     * 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如，输入字符串"abcdefg"和数字2，该函数将返回左旋转两位得到的结果"cdefgab"。
     * <p>
     * 示例 1：
     * 输入: s = "abcdefg", k = 2
     * 输出: "cdefgab"
     * <p>
     * 示例 2：
     * 输入: s = "lrloseumgh", k = 6
     * 输出: "umghlrlose"
     * <p>
     * 限制：1 <= k < s.length <= 10000
     */

    public String leftRotateStr(String s, int k) {

        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder tempBuilder = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            if (i < k) {
                tempBuilder.append(s.charAt(i));
            } else {
                stringBuilder.append(s.charAt(i));
            }
        }
        return stringBuilder.append(tempBuilder).toString();
    }


    public String leftRotateStr2(String s, int k) {

        char[] newChars = s.toCharArray();
        reverseChars(newChars, 0, k - 1);
        reverseChars(newChars, k, newChars.length - 1);
        reverseChars(newChars, 0, newChars.length - 1);
        return new String(newChars);
    }

    public void reverseChars(char[] chars, int head, int tail) {

        for (int i = head, j = tail; i < j; i++, j--) {
            char temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }
    }


    public static void main(String[] args) {
        LeftRotateStr leftRotateStr = new LeftRotateStr();
        String s = "abcdeft";
        //bacdeft
        //batfebc
        //cdeftab
        int k = 2;
        System.out.println(leftRotateStr.leftRotateStr2(s, 2));
    }
}